package com.wpf.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wpf.system.common.utils.UploadUtils;
import com.wpf.system.entity.Pictures;
import com.wpf.system.service.PicturesService;
import com.wpf.system.mapper.PicturesMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author ferna
* @description 针对表【pictures(图库表)】的数据库操作Service实现
* @createDate 2023-01-17 17:14:01
*/
@Service
public class PicturesServiceImpl extends ServiceImpl<PicturesMapper, Pictures> implements PicturesService{

    @Value("${system.saveFileUrl}")
    private String saveFileUrl;

    @Value("${server.port}")
    private String port;

    @Override
    public List<Pictures> getPicturesByBid(String bid) {
        return this.baseMapper.selectList(new QueryWrapper<Pictures>().lambda().eq(Pictures::getBid,bid));
    }

    @Override
    public int deletePicturesByIds(List<Long> ids) {
        int index = 0;
        for(Long id : ids ){
            index++;
            Pictures deletePictures = this.baseMapper.selectById(id);
            UploadUtils.deletePictures(deletePictures.getUrl(),saveFileUrl +":"+ port);
            if(index==ids.size()){
                UploadUtils.deleteDirectory(deletePictures.getBid());
            }
        }
        return this.baseMapper.deleteBatchIds(ids);
    }

    @Override
    public int deletePicturesByBid(List<String> bidList) {
        return this.baseMapper.deletePicturesByBid(bidList);
    }

    @Override
    public Boolean deleteAllFiles(String bid) {
        return UploadUtils.deleteAllFiles(bid,"img");
    }
}




